<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 商铺管理
 */
class Store_mdl extends CI_Model {

    const TABLE = 'store';

    public function select(){
        return $this->db->get(self::TABLE)->result_array();
    }

    public function get_one(){
        return $this->db->get(self::TABLE)->row_array();
    }

    //统计
    public function get_count(){
        return $this->db->count_all_results(self::TABLE);
    }

    //添加
    public function create(){
        return $this->db->insert(self::TABLE);
    }
    //修改
    public function update(){
        return $this->db->update(self::TABLE);
    }

    //组装数据
    public function restr($list){
        $this->load->model('goods_mdl');

        foreach ($list as $k => $v){
            //统计商铺的所有商品
            $this->db->where('store_id',$v['id']);
            $this->db->where('status !=',0);
            $list[$k]['goods_count'] = $this->goods_mdl->get_count();

        }

        return $list;
    }

    //店铺删除
    public function delete($sid = 0){
        if(empty($sid)){
            return false;
        }

        //删除该店铺的所有管理
        $this->load->model('suser_mdl');
        $this->db->where('store_id',$sid);
        $this->suser_mdl->delete();

        //删除该店铺下的所有商品
        $this->load->model('goods_mdl');
        $this->db->select('id');
        $this->db->where('store_id',$sid);
        $list = $this->goods_mdl->select();

        foreach($list as $k => $v){
            $this->goods_mdl->delete($v['id']);
        }
        //删除商铺图片
        $this->db->where('id',$sid);
        $res_store = $this->get_one();
        
        if(file_exists('.'.$res_store['logo'])){
            @unlink('.'.$res_store['logo']);
        }
        if(file_exists('./upload/store/original/'.substr($res_store['logo'],26))){
            @unlink('./upload/store/original/'.substr($res_store['logo'],26));
        }

        //删除店铺
        $this->db->where('id',$sid);
        $res = $this->db->delete(self::TABLE);

        return $res;

    }
}

/* End of file store_mdl.php */
/* Location: ./application/models/store_mdl.php */